package cn.edu.sgu.www.mhxysy.service.zuoqi.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.zuoqi.ZuoqiCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.zuoqi.ZuoqiCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.zuoqi.ZuoqiCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.pager.zuoqi.ZuoqiCategoryPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.zuoqi.ZuoqiCategoryService;
import cn.edu.sgu.www.mhxysy.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ZuoqiCategoryServiceImpl implements ZuoqiCategoryService {

	private final UploadUtils uploadUtils;
	private final ImageUpdater imageUpdater;
	private final ZuoqiCategoryMapper zuoqiCategoryMapper;

	@Autowired
	public ZuoqiCategoryServiceImpl(
			UploadUtils uploadUtils,
			ImageUpdater imageUpdater,
			ZuoqiCategoryMapper zuoqiCategoryMapper) {
		this.uploadUtils = uploadUtils;
		this.imageUpdater = imageUpdater;
		this.zuoqiCategoryMapper = zuoqiCategoryMapper;
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		String directory = CustomDirectories.DIRECTORY_ZUOQI;

		return uploadUtils.upload(file, directory);
	}

	@Override
	public void insert(ZuoqiCategoryDTO zuoqiCategoryDTO) {
		ZuoqiCategory zuoqiCategory = toEntity(zuoqiCategoryDTO);

		zuoqiCategory.setId(null);

		zuoqiCategoryMapper.insert(zuoqiCategory);
	}

	@Override
	public void deleteById(Integer id) {
		zuoqiCategoryMapper.deleteById(id);
	}

	@Override
	public void updateById(ZuoqiCategoryDTO zuoqiCategoryDTO) {
		ObjectUtils.requireNonNull(zuoqiCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

		ZuoqiCategory zuoqiCategory = toEntity(zuoqiCategoryDTO);

		zuoqiCategoryMapper.updateById(zuoqiCategory);
	}

	@Override
	public void updateImageById(Integer id, String image) {
		imageUpdater.updateImageById(image, new Consumer<Object>() {
			@Override
			public void accept(Object o) {
				UpdateWrapper<ZuoqiCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

				zuoqiCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
			}
		});
	}

	@Override
	public List<ZuoqiCategory> selectAll() {
		return zuoqiCategoryMapper.selectList(null);
	}

	@Override
	public ZuoqiCategory selectById(Integer id) {
		return zuoqiCategoryMapper.selectById(id);
	}

	@Override
	public Page<ZuoqiCategory> selectByPage(ZuoqiCategoryPager pager) {
		QueryWrapper<ZuoqiCategory> queryWrapper = new QueryWrapper<>();
		Page<ZuoqiCategory> page = Pager.ofPage(pager);

		queryWrapper.like(
				StringUtils.isNotEmpty(pager.getName()),
				"name", pager.getName()
		);

		return zuoqiCategoryMapper.selectPage(page, queryWrapper);
	}

	private ZuoqiCategory toEntity(ZuoqiCategoryDTO zuoqiCategoryDTO) {
		// 验证坐骑名称重复
		QueryWrapper<ZuoqiCategory> queryWrapper = new QueryWrapper<>();

		queryWrapper.ne(
				zuoqiCategoryDTO.getId() != null,
				"id", zuoqiCategoryDTO.getId()
		);
		queryWrapper.eq("name", zuoqiCategoryDTO.getName());

		Long count = zuoqiCategoryMapper.selectCount(queryWrapper);

		if (count > 0) {
			throw new GlobalException(ResponseCode.CONFLICT, "坐骑名称重复！");
		}

		// 转为数据库实体类类型
		ZuoqiCategory zuoqiCategory = new ZuoqiCategory();

		zuoqiCategory.setId(zuoqiCategoryDTO.getId());
		zuoqiCategory.setName(zuoqiCategoryDTO.getName());
		zuoqiCategory.setType(zuoqiCategoryDTO.getType());
		zuoqiCategory.setFashion(zuoqiCategoryDTO.getFashion());

		return zuoqiCategory;
	}

}